<template>
  <div class="app-container">
    <div class="news-container">
      <div class="header-section">
        <h2 class="page-title">家教资讯</h2>
        <div class="action-buttons">
          <el-button type="primary" @click="goHome">返回主页</el-button>
          <el-select v-model="sortOrder" placeholder="排序方式" @change="handleSortChange" style="margin-left: 10px;">
            <el-option label="最新发布" value="desc"></el-option>
            <el-option label="最早发布" value="asc"></el-option>
          </el-select>
        </div>
      </div>
      
      <div class="news-list">
        <el-card v-for="(item, index) in newsList" :key="index" class="news-item" @click.native="goToDetail(item.newsId)">
          <div class="news-content">
            <h3 class="news-title">{{ item.title }}</h3>
            <div class="news-info">
              <span class="time">{{ parseTime(item.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
              <!-- <span class="author">{{ item.authorId }}</span> -->
            </div>
            <div class="news-summary" v-html="item.content"></div>
          </div>
        </el-card>
      </div>

      <div class="pagination-container" v-if="total > 0">
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryParams.pageNum"
          :page-sizes="[10, 20, 30, 50]"
          :page-size="queryParams.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import { listNews } from "@/api/news/news";

export default {
  name: 'News',
  data() {
    return {
      newsList: [],
      total: 0,
      sortOrder: 'desc', // 默认按最新发布排序
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        content: null,
        authorId: null,
        orderByColumn: 'createTime', // 排序字段
        isAsc: 'desc' // 默认降序
      }
    }
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      listNews(this.queryParams).then(response => {
        this.newsList = response.rows;
        this.total = response.total;
      });
    },
    handleSizeChange(val) {
      this.queryParams.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.queryParams.pageNum = val;
      this.getList();
    },
    goToDetail(newsId) {
      this.$router.push({
        path: '/news/detail',
        query: {
          id: newsId
        }
      });
    },
    goHome() {
      this.$router.push('/userHome');
    },
    handleSortChange(value) {
      this.sortOrder = value;
      this.queryParams.isAsc = value;
      this.getList();
    }
  }
}
</script>

<style lang="scss" scoped>
.news-container {
  padding: 30px;
  min-height: 100vh;
  background: linear-gradient(135deg, #f6f8fc 0%, #f1f4f9 100%);

  .header-section {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 30px;
    padding: 20px;
    background: rgba(255, 255, 255, 0.9);
    border-radius: 16px;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.06);
    backdrop-filter: blur(10px);
    border: 1px solid rgba(255, 255, 255, 0.8);

    .page-title {
      margin: 0;
      font-size: 28px;
      color: #2c3e50;
      font-weight: 600;
      position: relative;
      padding-left: 15px;
      border-left: 4px solid #e61e1e;
      text-shadow: 0 2px 4px rgba(0,0,0,0.05);
    }

    .action-buttons {
      display: flex;
      align-items: center;
      gap: 15px;

      .el-button {
        padding: 10px 20px;
        border-radius: 8px;
        transition: all 0.3s ease;

        &:hover {
          transform: translateY(-2px);
          box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
        }
      }

      .el-select {
        .el-input__inner {
          border-radius: 8px;
          border-color: #dcdfe6;
          transition: all 0.3s ease;

          &:hover, &:focus {
            border-color: #e61e1e;
          }
        }
      }
    }
  }

  .news-list {
    .news-item {
      margin-bottom: 25px;
      cursor: pointer;
      transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
      border: none;
      background: rgba(255, 255, 255, 0.9);
      backdrop-filter: blur(10px);
      border-radius: 16px;
      overflow: hidden;
      border: 1px solid rgba(255, 255, 255, 0.8);

      &:hover {
        transform: translateY(-8px);
        box-shadow: 0 12px 30px rgba(0, 0, 0, 0.1);

        .news-content {
          .news-title {
            color: #e61e1e;
          }

          .news-summary {
            &::after {
              opacity: 0;
            }
          }
        }
      }

      .news-content {
        padding: 25px;

        .news-title {
          margin: 0 0 15px;
          font-size: 20px;
          color: #2c3e50;
          font-weight: 600;
          transition: color 0.3s ease;
          line-height: 1.4;
        }

        .news-info {
          margin-bottom: 15px;
          font-size: 14px;
          color: #909399;
          display: flex;
          align-items: center;
          gap: 20px;

          .time {
            display: flex;
            align-items: center;
            gap: 6px;
            
            &::before {
              content: '\e78f';
              font-family: element-icons;
              font-size: 16px;
            }
          }
        }

        .news-summary {
          margin: 0;
          color: #606266;
          line-height: 1.6;
          max-height: 100px;
          overflow: hidden;
          position: relative;
          transition: all 0.3s ease;

          &::after {
            content: '';
            position: absolute;
            bottom: 0;
            left: 0;
            right: 0;
            height: 40px;
            background: linear-gradient(to bottom, rgba(255,255,255,0), rgba(255,255,255,1));
            transition: opacity 0.3s ease;
          }

          :deep(p) {
            margin: 0;
            display: -webkit-box;
            -webkit-box-orient: vertical;
            -webkit-line-clamp: 3;
            overflow: hidden;
            line-clamp: 3;
          }

          :deep(img) {
            max-width: 100%;
            height: auto;
            border-radius: 8px;
            margin: 10px 0;
          }
        }
      }
    }
  }

  .pagination-container {
    margin-top: 30px;
    text-align: center;
    padding: 20px;
    background: rgba(255, 255, 255, 0.9);
    border-radius: 16px;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.06);
    backdrop-filter: blur(10px);
    border: 1px solid rgba(255, 255, 255, 0.8);

    :deep(.el-pagination) {
      .el-pagination__total,
      .el-pagination__jump {
        color: #606266;
      }

      .btn-prev,
      .btn-next,
      .el-pager li {
        background: transparent;
        border: 1px solid #dcdfe6;
        border-radius: 8px;
        transition: all 0.3s ease;

        &:hover {
          color: #e61e1e;
          border-color: #e61e1e;
        }

        &.active {
          background: #e61e1e;
          color: white;
          border-color: #e61e1e;
        }
      }
    }
  }
}

// 添加响应式布局
@media screen and (max-width: 768px) {
  .news-container {
    padding: 20px;

    .header-section {
      flex-direction: column;
      gap: 15px;
      text-align: center;

      .page-title {
        border-left: none;
        padding-left: 0;
        border-bottom: 4px solid #e61e1e;
        padding-bottom: 10px;
      }
    }

    .news-list .news-item {
      margin-bottom: 20px;
    }
  }
}
</style>